<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;

class Cuser extends Controller
{
    //首页
    public function index()
    {
        $id = Session::get('id');

        $list = Db::field(['b.username', 'b.id', 'p.name', 'p.uid'])
            ->table(['wb_buser' => 'b', 'wb_photofolder' => 'p'])
            ->where('b.id=1 and b.id = p.uid')
            ->select();

        $p = Db::field('photo')
            ->table(['wb_buserinfo'])
            ->where('uid = '.$id)
            ->find();
        $photo = implode("",$p);

        $message = Db::table('wb_message m')
            ->join( 'wb_buser b','m.uid=b.id','left')
            ->join('wb_mpicture p','p.mid=m.id ','left')
            ->join('wb_buserinfo i','b.id=i.uid','left')
            ->field(['m.content', 'm.time', 'm.collectnum', 'm.commentnum', 'm.transpondnum', 'm.agreenum', 'm.readnum', 'b.username','m.id','group_concat(p.path) path','i.photo','src_id'])
            ->where('b.id=' . $id)
            ->where('m.uid=' . $id)
            ->order('id','desc')
            ->group('m.id')
            ->select();

        foreach ($message as $k => $v) {

            if (!empty($v['src_id'])){
                $res1 = Db::field('m.id id,u.username username,photo,content,time,m.uid uid')
                    ->table(['wb_message' => 'm', 'wb_buser' => 'u','wb_buserinfo'=>'i'])
                    ->where('m.uid = u.id and  i.uid = u.id  and  m.id = '.$v['src_id'])
                    ->find();
                $message[$k]['old'] = $res1;
                $path = Db::field('path')
                    ->table(['wb_message' => 'm','wb_mpicture'=>'mp'])
                    ->where('m.id = '.$v['src_id'].' and mp.mid = m.id')
                    ->find();
                if (!empty($path)){
                    $message[$k]['old']['path'] = $path;
                }
            }
        }

        $attent = Db::table('wb_attent a')
            ->join('wb_buser b', 'a.attent_id=b.id')
            ->join('wb_buserinfo i', 'i.uid=b.id')
            ->field(['i.photo', 'b.username'])
            ->where('a.user_id=' . $id)
            ->select();

        $fans = Db::table('wb_fans f')
            ->join('wb_buser b', 'f.fans_id=b.id')
            ->join('wb_buserinfo i', 'i.uid=b.id')
            ->field(['i.photo', 'b.username'])
            ->where('f.user_id=' . $id)
            ->select();

        $at = Db::field('uid,title,thumbnail,id')
            ->table('wb_articles')
            ->select();

        $username = Session::get('username');
        return view('index@cuser/index', [
            'list' => $list,
            'username' =>$username,
            'photo' => $photo,
            'message'=> $message,
            'at' => $at,
            'fans'=>$fans,
            'attent'=>$attent,
        ]);
    }

    //相册
    public function cphoto()
    {
        $id = Session::get('id');

        $res = Db::field(['b.username', 'b.id','p.name','p.uid'])
            ->table(['wb_buser'=>'b','wb_photofolder' =>'p'])
            ->where('b.id = p.uid and b.id = '.$id)
            ->select();

        $username = Session::get('username');


        $p = Db::field('photo')
            ->table(['wb_buserinfo'])
            ->where('uid = '.$id)
            ->find();
        $photo = implode("",$p);

        return view('index@cuser/cphoto', [
            'res' => $res,
            'username' =>$username,
            'photo' => $photo,
        ]);
    }

    //编辑页面
    public function edit()
    {
        $username = Session::get('username');
        $id = Session::get('id');

        $t = Db::field('tel')
            ->table(['wb_buser'])
            ->where('id = '.$id)
            ->find();
        $tel = implode("",$t);

        $e = Db::field('email')
            ->table(['wb_buserinfo'])
            ->where('uid = '.$id)
            ->find();
        $email = implode("",$e);

        $q = Db::field('qq')
            ->table(['wb_buserinfo'])
            ->where('uid = '.$id)
            ->find();
        $qq = implode("",$q);

        $p = Db::field('photo')
            ->table(['wb_buserinfo'])
            ->where('uid = '.$id)
            ->find();
        $photo = implode("",$p);

        return view('index@cuser/edit',[
            'username' => $username,
            'tel' => $tel,
            'email' => $email,
            'qq' => $qq,
            'photo' =>$photo,
        ]);
    }

    //编辑资料
    public function doEdit()
    {
        $username = input("param.username");
        $tel = input("param.tel");
        $email = input("param.email");
        $qq = input("param.qq");
        $id = Session::get('id');


        session('username', $username);


        $file = request()->file('photo');

        $r = Db::field('role')
            ->table(['wb_buser'])
            ->where('id = '.$id)
            ->find();
        $role = implode("",$r);



            $src = '';
        if ( $file !== NULL ){
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
            $src =  '/upload' . '/' . date('Ymd') . '/' . $info->getFilename();
        }else{
            $src = '/upload/20190214/5f9cf34f975de2583d63b42e193aecb6.png';
        }
        // 页面接收的数据(以post方式传输 路由方式为post)
        $p = input('post.');
        // 对数据进行验证, 不符合返回页面
        $check = $this->validate($p, 'index/EditValidate');
        if ($check !== true) {
            $this->error($check);
            return;
        }

        db('buser')->where('id',$id)->update(['username' => $username,'tel'=>$tel]);
        db('buserinfo')->where('uid',$id)->update(['email' => $email,'qq'=>$qq,'photo'=>$src]);
        if ($role == 1){
            $this->success('保存成功','index/cuser/index');
        } else {
            $this->success('保存成功', 'index/buser/index');
        }
    }

   

    //插入数据库
    public function inchat()
    {
        $username = Session::get('username');

        $id = Session::get('id');
        $sid = $_POST['sid'];
        $chat = $_POST['mychat'];

        $po = Db::field('photo')
            ->table(['wb_buserinfo'])
            ->where('uid = '.$id)
            ->find();
        $myphoto = implode("",$po);

        $p = Db::field('photo')
            ->table(['wb_buserinfo'])
            ->where('uid = 60')
            ->find();
        $photo = implode("",$p);

        $us = Db::field('username')
            ->table(['wb_buser'])
            ->where('id = '.$id)
            ->find();
        $myusername = implode("",$us);

        $f = Db::field('content')
            ->table(['wb_fchat'])
            ->where('id = '.$id)
            ->select();

        $s = Db::field('content')
            ->table(['wb_schat'])
            ->where('id = 60')
            ->select();


        $data = [
            'id' => $id,
            'sid' => $sid,
            'content' => $chat,
        ];
        db()->table('wb_fchat')->insertGetId($data);
        return view('/cuser/chat',[
            'username' => $username,
            'photo' => $photo,
            'myphoto' => $myphoto,
            'myusername' => $myusername,
            'f' => $f,
            's' => $s,
            'id' => $id,
        ]);
    }

    //私聊
    public function chat()
    {
        $id = Session::get('id');
        $u = Db::field('username')
            ->table(['wb_buser'])
            ->where('id = 60')
            ->find();
        $username = implode("",$u);

        $p = Db::field('photo')
            ->table(['wb_buserinfo'])
            ->where('uid = 60')
            ->find();
        $photo = implode("",$p);

        $po = Db::field('photo')
            ->table(['wb_buserinfo'])
            ->where('uid = '.$id)
            ->find();
        $myphoto = implode("",$po);

        $us = Db::field('username')
            ->table(['wb_buser'])
            ->where('id = '.$id)
            ->find();
        $myusername = implode("",$us);

        $f = Db::field('content')
            ->table(['wb_fchat'])
            ->where('id = '.$id)
            ->select();

        $s = Db::field('content')
            ->table(['wb_schat'])
            ->where('id = 60')
            ->select();

        return view('index@cuser/chat',[
            'username' => $username,
            'photo' => $photo,
            'myphoto' => $myphoto,
            'myusername' => $myusername,
            'f' => $f,
            's' => $s,
            'id' => $id,
        ]);
    }

    //删除微博
    public function delWeibo()
    {

        $id = $_GET['id'];

        $res=Db::table('wb_message')
            ->where('id='.$id)
            ->delete();

        return join([
            'res' => $res,
            'id'=>$id
        ]);
    }


    public function editpass()
    {
        $id = Session::get('id');
        $username = Session::get('username');
        return view('index@cuser/editpass',[
            'username' => $username,
            'id' => $id,
        ]);
    }

    public function doeditpass()
    {
        $p = $_POST['password'];
        $p = md5($p);
        $r = $_POST['repassword'];
        $r = md5($r);
        $id = $_POST['id'];
        $pa = Db::field('password')
            ->table('wb_buser')
            ->where('id = '.$id )
            ->find();
        $mypa = implode("",$pa);
        $r = Db::field('role')
            ->table(['wb_buser'])
            ->where('id = '.$id)
            ->find();
        $role = implode("",$r);
        if ($p !== $mypa ){
            $this->error('请填写正确的原密码');
        }

        Db::table('wb_buser')->where('id', $id)->update(['password' => $r]);
        if ($role == 1){
            $this->success('修改成功','index/cuser/index');
        }else{
            $this->success('修改成功','index/buser/index');
        }
    }
}